#!/bin/bash
################################################################################################################################
# automatically read from programschedule table, and compose manual recommendation rules and insert into manualprogram table
# Parts: there are 2 parts for this script, Part A is for program, and Part B is for series
# NOTE: if there are 2 scheduled programs, then the top 20 hot programs will be used to recommend these 2, and if there are 3,
#       then 30 hot programs will be used. To adjust this batch size, alter BATCH_SIZE in this script.
################################################################################################################################

MYSQL_USER='root'
MYSQL_PASSWORD='mysql'
MYSQL_HOST='10.61.13.96'
MYSQL_DB='recommendationengine_fuzhou'
BATCH_SIZE=10 # how many hot programs will be used to recommend a single specified program in schedule table
debug=false

debug() {
    if [ $debug = true ]
    then
        echo '[debug] ' $1 $2 $3 $4
    fi
}
info() {
    echo '[info]' $1 $2 $3 $4
}

############ Part A: for program #############
info 'truncate table...'
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -h$MYSQL_HOST -e"truncate $MYSQL_DB.manualprogram"

info 'get manual recommendation schedule...'
currentSchedule=`mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -h$MYSQL_HOST -e"select programid from $MYSQL_DB.manualprogramschedule where now()-starttime=(select min(now()-starttime) from recommendationengine_fuzhou.manualprogramschedule where now()-starttime>0) order by pos" | grep -v programid`
debug "current schedules: $currentSchedule"

num=0 # record the number of selected schedule
for i in $currentSchedule
do
    num=$((num+1))
done
info "number of schedule is $num."

info 'begin to insert manual recommendation rules...'
i=0
for s in $currentSchedule
do
    hotprogram=`mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -h$MYSQL_HOST -e"select programid from $MYSQL_DB.hotprogrambytype where type=2 order by viewtimes desc limit $((num*$BATCH_SIZE))"  | grep -v programid | awk NR%$num==$i`
    i=$((i+1))
    debug "hotprogram batch $i: $hotprogram"
    info "inserting into manualprogram table for batch $i hot programs..."
    pos=1
    for h in $hotprogram
    do
       debug "inserting manual rule: $h --> $s"
       mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -h$MYSQL_HOST -e"insert into $MYSQL_DB.manualprogram values(null, '$h', '$s', $pos)"
       pos=$((pos+1))
    done
done
info 'program part is done!'


############ Part B: for series #############
info 'truncate table...'
mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -h$MYSQL_HOST -e"truncate $MYSQL_DB.manualseries"

info 'get manual recommendation schedule...'
currentSchedule=`mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -h$MYSQL_HOST -e"select seriesid from $MYSQL_DB.manualseriesschedule where now()-starttime=(select min(now()-starttime) from recommendationengine_fuzhou.manualseriesschedule where now()-starttime>0) order by pos" | grep -v seriesid`
debug "current schedules: $currentSchedule"

num=0 # record the number of selected schedule
for i in $currentSchedule
do
    num=$((num+1))
done
info "number of schedule is $num."

info 'begin to insert manual recommendation rules...'
i=0
for s in $currentSchedule
do
    hotseries=`mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -h$MYSQL_HOST -e"select seriesid from $MYSQL_DB.hotseriesbytype where type=1 order by viewtimes desc limit $((num*$BATCH_SIZE))"  | grep -v seriesid | awk NR%$num==$i`
    i=$((i+1))
    debug "hotseries batch $i: $hotseries"
    info "inserting into manualseries table for batch $i hot seriess..."
    pos=1
    for h in $hotseries
    do
       debug "inserting manual rule: $h --> $s"
       mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -h$MYSQL_HOST -e"insert into $MYSQL_DB.manualseries values(null, '$h', '$s', $pos)"
       pos=$((pos+1))
    done
done
info 'series part is done!'

info 'done!'
info '!!!!!! DO NOT need to restart tomcat any more...!!!!!!'


